package com.zhuozhou.jdyc.main.meinfo.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.RegexUtils;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.bean.PopupWindowBean;
import com.zhuozhou.jdyc.databinding.ActivityRegisteredAgentBinding;
import com.zhuozhou.jdyc.main.base.adapter.ImageAddAdapter;
import com.zhuozhou.jdyc.main.meinfo.adapter.CheckBoxAdapter;
import com.zhuozhou.jdyc.main.meinfo.bean.CheckBoxBean;
import com.zhuozhou.jdyc.main.meinfo.bean.DaiLiShangBean;
import com.zhuozhou.jdyc.main.meinfo.bean.QuYuBean;
import com.zhuozhou.jdyc.main.meinfo.viewmodel.MeInfoViewModel;
import com.zhuozhou.jdyc.util.apiUtil.DateUtil;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;
import com.zhuozhou.jdyc.util.media.MediaBuilder;
import com.zhuozhou.jdyc.util.media.MediaHelper;
import com.zhuozhou.jdyc.util.media.MediaTypeEnum;
import com.zhuozhou.jdyc.wight.dialog.OpenImageDialog;
import com.zhuozhou.jdyc.wight.dialog.PickStringDialog;
import com.zhuozhou.jdyc.wight.dialog.PickTimeViewDialog;
import com.zhuozhou.jdyc.wight.pickertime.PickTimeView;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/10/11
 * Describe:
 */
public class RegisteredAgentActivity extends BaseActivity<MeInfoViewModel, ActivityRegisteredAgentBinding> {
    private ImageAddAdapter imageAddAdapter1;
    private ImageAddAdapter imageAddAdapter2;
    private ImageAddAdapter imageAddAdapter3;
    private MediaHelper mediaHelper1;
    private MediaHelper mediaHelper2;
    private MediaHelper mediaHelper3;
    private OpenImageDialog openImageDialog;
    private CheckBoxAdapter checkBoxAdapter;
    private PickStringDialog pickStringDialog;
    private PickTimeViewDialog pickTimeViewDialog;
    private List<PopupWindowBean> characterList;
    private List<PopupWindowBean> leiXingList;
    private List<PopupWindowBean> weiXinList;
    private Double Latitude;
    private Double Longitude;
    private String AdName;

    @Override
    public String setTitleBar() {
        return "成为代理商";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_registered_agent;
    }

    @Override
    public void initView() {
        mViewModel.QuYuMutableLiveData.observe(this, v -> {
            characterList = new ArrayList<>();
            for (QuYuBean.ArrayBean item : v.getArray()) {
                characterList.add(new PopupWindowBean(item.getId(), item.getNickname()));
            }
        });
        binding.CharacterSet.setOnClickListener(v -> {
            if (characterList != null) {
                pickStringDialog = new PickStringDialog(this, 1, characterList, (mCurrentPick, position, showText) -> {
                    binding.CharacterSet.setText(showText);
                    binding.CharacterSet.setTag(position);
                });
                pickStringDialog.create(null).show();
            }
        });
        mViewModel.PeiSongMutableLiveData.observe(this, v -> {
            checkBoxAdapter = new CheckBoxAdapter(this);
            checkBoxAdapter.setList(v.getArray());
            binding.mRecyclerViewCheck.setAdapter(checkBoxAdapter);
            binding.mRecyclerViewCheck.setLayoutManager(new MyLayoutManager(this));
        });
        mViewModel.DianPuLeiXingMutableLiveData.observe(this, v -> {
            leiXingList = new ArrayList<>();
            for (CheckBoxBean.ArrayBean item : v.getArray()) {
                leiXingList.add(new PopupWindowBean(item.getId(), item.getName()));
            }
        });
        binding.storeType.setOnClickListener(v -> {
            if (leiXingList != null) {
                pickStringDialog = new PickStringDialog(this, 1, leiXingList, (mCurrentPick, position, showText) -> {
                    binding.storeType.setText(showText);
                    binding.storeType.setTag(position);
                });
                pickStringDialog.create(null).show();
            }
        });
        binding.startTime.setOnClickListener(v -> {
            pickTimeViewDialog = new PickTimeViewDialog(this);
            pickTimeViewDialog.setViewType(PickTimeView.TYPE_PICK_TIME);
            pickTimeViewDialog.setOnPickTimeDateSelectedListener((pickTimeViewDialog, date) -> {
                pickTimeViewDialog.dismiss();
                binding.startTime.setText(DateUtil.getTimeHourFormat(new Date(date)));
            });
            pickTimeViewDialog.create();
            pickTimeViewDialog.show();
        });
        binding.endTime.setOnClickListener(v -> {
            pickTimeViewDialog = new PickTimeViewDialog(this);
            pickTimeViewDialog.setViewType(PickTimeView.TYPE_PICK_TIME);
            pickTimeViewDialog.setOnPickTimeDateSelectedListener((pickTimeViewDialog, date) -> {
                pickTimeViewDialog.dismiss();
                binding.endTime.setText(DateUtil.getTimeHourFormat(new Date(date)));
            });
            pickTimeViewDialog.create();
            pickTimeViewDialog.show();
        });
        binding.StoreAddress.setOnClickListener(v -> {
            Intent in = new Intent(this, MapMakerActivity.class);
            startActivityForResult(in, 10001);
        });
        mViewModel.WeiXinMutableLiveData.observe(this, v -> {
            List<PopupWindowBean> list = new ArrayList<>();
            for (CheckBoxBean.ArrayBean item : v.getArray()) {
                list.add(new PopupWindowBean(item.getId(), item.getOpenname()));
            }
            weiXinList = new ArrayList<>();
            weiXinList.addAll(list);
        });
        binding.BindingWeChat.setOnClickListener(v -> {
            if (weiXinList != null) {
                pickStringDialog = new PickStringDialog(this, 2, weiXinList, (mCurrentPick, position, showText) -> {
                    binding.BindingWeChat.setText(showText);
                    binding.BindingWeChat.setTag(position);
                });
                pickStringDialog.setOnSearchEditListener(showText -> {
                    mViewModel.getWeiXin(showText, false);
                });
                pickStringDialog.create(null).show();
            }
        });

        binding.submit.setOnClickListener(v -> {
            if (StringUtil.isEmpty(binding.CharacterSet.getText().toString())) {
                ToastUtils.showShort(this, "请选择角色组");
                return;
            }
            if (getCheckBox(checkBoxAdapter.getList()).size() == 0) {
                ToastUtils.showShort(this, "请选择配送方式");
                return;
            }
            if (StringUtil.isEmpty(binding.userName.getText().toString())) {
                ToastUtils.showShort(this, "请输入用户名");
                return;
            }
            if (StringUtil.isEmpty(binding.nickname.getText().toString())) {
                ToastUtils.showShort(this, "请输入昵称");
                return;
            }
            if (StringUtil.isEmpty(binding.password1.getText().toString())) {
                ToastUtils.showShort(this, "请输入密码");
                return;
            }
            if (StringUtil.isEmpty(binding.password2.getText().toString())) {
                ToastUtils.showShort(this, "请再次输入密码");
                return;
            }
            if (binding.password1.getText().toString().equals(binding.password2.getText().toString())) {
                ToastUtils.showShort(this, "两次输入密码不一致");
                return;
            }
            if (StringUtil.isEmpty(binding.storeType.getText().toString())) {
                ToastUtils.showShort(this, "请选择店铺类型");
                return;
            }

            if (StringUtil.isEmpty(binding.startTime.getText().toString())) {
                ToastUtils.showShort(this, "请选择营业时间");
                return;
            }
            if (StringUtil.isEmpty(binding.endTime.getText().toString())) {
                ToastUtils.showShort(this, "请选择结束时间");
                return;
            }
            if (StringUtil.isEmpty(binding.StoreAddress.getText().toString())) {
                ToastUtils.showShort(this, "请选择店铺地址");//收货地址
                return;
            }
            if (imageAddAdapter1.getList().size() == 0) {
                ToastUtils.showShort(this, "请上传有效证件");
                return;
            }
            if (imageAddAdapter2.getList().size() == 0) {
                ToastUtils.showShort(this, "请上传身份证件");
                return;
            }
            if (imageAddAdapter3.getList().size() == 0) {
                ToastUtils.showShort(this, "请上传店铺图片");
                return;
            }
            if (StringUtil.isEmpty(binding.depositBank.getText().toString())) {
                ToastUtils.showShort(this, "请输入开户银行");
                return;
            }
            if (StringUtil.isEmpty(binding.BankCardNumber.getText().toString())) {
                ToastUtils.showShort(this, "请输入银行卡号");
                return;
            }
            if (StringUtil.isEmpty(binding.AlipayAccount.getText().toString())) {
                ToastUtils.showShort(this, "请输入支付宝账号");
                return;
            }
            if (!RegexUtils.isMobile(binding.AlipayAccount.getText().toString())) {
                ToastUtils.showShort(this, "支付宝账号不正确");
                return;
            }
            if (StringUtil.isEmpty(binding.BindingWeChat.getText().toString())) {
                ToastUtils.showShort(this, "请选择微信");
                return;
            }
            if (StringUtil.isEmpty(binding.BindWechatMobile.getText().toString())) {
                ToastUtils.showShort(this, "请输入绑定微信手机号");
                return;
            }
            if (!RegexUtils.isMobile(binding.BindWechatMobile.getText().toString())) {
                ToastUtils.showShort(this, "微信手机号不正确");
                return;
            }

            DaiLiShangBean daiLiShangBean = new DaiLiShangBean();
            daiLiShangBean.setShenfen(Integer.parseInt(binding.CharacterSet.getTag().toString()));
            daiLiShangBean.setStatus_type(getCheckBox(checkBoxAdapter.getList()).toString());
            daiLiShangBean.setUsername(binding.userName.getText().toString());
            daiLiShangBean.setNickname(binding.nickname.getText().toString());
            daiLiShangBean.setPassword(binding.password1.getText().toString());
            daiLiShangBean.setMy2type_id(Integer.parseInt(binding.storeType.getTag().toString()));
            daiLiShangBean.setStartTime(binding.startTime.getTag().toString());
            daiLiShangBean.setEndTime(binding.endTime.getTag().toString());
            daiLiShangBean.setEndTime(binding.endTime.getTag().toString());
            daiLiShangBean.setEndTime(binding.endTime.getTag().toString());
            daiLiShangBean.setBank(binding.depositBank.getText().toString());
            daiLiShangBean.setBanknumber(binding.BankCardNumber.getText().toString());
            daiLiShangBean.setZhifubao(binding.AlipayAccount.getText().toString());
            daiLiShangBean.setWx(binding.BindingWeChat.getText().toString());
            daiLiShangBean.setWx_user_id(Integer.parseInt(binding.BindingWeChat.getTag().toString()));
            daiLiShangBean.setWx_phone(binding.BindWechatMobile.getText().toString());
            daiLiShangBean.setIdentity(imageAddAdapter1.getList().toString());
            daiLiShangBean.setImages(imageAddAdapter2.getList().toString());
            daiLiShangBean.setDianpuimg(imageAddAdapter3.getList().toString());
            daiLiShangBean.setSite(binding.StoreAddress.getText().toString());
            daiLiShangBean.setX(Latitude);
            daiLiShangBean.setY(Longitude);
            daiLiShangBean.setCity(UserAccountHelper.getNewCity());
            daiLiShangBean.setYewuyuan(binding.salesmanName.getText().toString());
            daiLiShangBean.setPhone(binding.salesmanTelephone.getText().toString());
            mViewModel.postDaiLiShang(daiLiShangBean);
        });
        mViewModel.DaiLiShangMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(v.getMsg());
            finish();
        });
        //有效证件
        imageAddAdapter1 = new ImageAddAdapter(this, 1);
        mediaHelper1 = new MediaBuilder(this, this)
                .setImageAlbumCount(1)
                .setImageQualityCompress(0)
                .builder();
        mediaHelper1.getMutableLiveData().observe(this, mediaBean -> {
            if (mediaBean.getMediaType() == MediaTypeEnum.IMAGE.getMediaType()) {
                for (String url : mediaBean.getMediaList()) {
                    mViewModel.postDentity("https://yuncang.dzsmxx.com/api/timetable/Basics/updateimg", new File(url));
                }
            }
        });
        mViewModel.DentityMutableLiveData.observe(this, v -> {
            imageAddAdapter1.getList().add("https://yuncang.dzsmxx.com" + v.getObj());
            imageAddAdapter1.notifyDataSetChanged();
            mediaHelper1.setCurrentImageCount(imageAddAdapter1.getList().size());
        });
        imageAddAdapter1.setImageViewAddListener(view -> {
            openImageDialog = new OpenImageDialog(context);
            openImageDialog.setMediaType(OpenImageDialog.ALBUM)
                    .setOnOpenImageClickListener(mediaHelper1)
                    .builder()
                    .show();
        });
        imageAddAdapter1.setImageViewClearListener((view, position) -> {
            imageAddAdapter1.getList().remove(position);
            imageAddAdapter1.notifyDataSetChanged();
            mediaHelper1.setCurrentImageCount(imageAddAdapter1.getList().size());
        });
        binding.documentsRecyclerView.setLayoutManager(new FullyGridLayoutManager(this, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        binding.documentsRecyclerView.setAdapter(imageAddAdapter1);
        //身份证件
        imageAddAdapter2 = new ImageAddAdapter(this, 1);
        mediaHelper2 = new MediaBuilder(this, this)
                .setImageAlbumCount(1)
                .setImageQualityCompress(0)
                .builder();
        mediaHelper2.getMutableLiveData().observe(this, mediaBean -> {
            if (mediaBean.getMediaType() == MediaTypeEnum.IMAGE.getMediaType()) {
//                imageAddAdapter2.getList().addAll(mediaBean.getMediaList());
//                imageAddAdapter2.notifyDataSetChanged();
//                mediaHelper2.setCurrentImageCount(imageAddAdapter2.getList().size());
                for (String url : mediaBean.getMediaList()) {
                    mViewModel.postImages("https://yuncang.dzsmxx.com/api/timetable/Basics/updateimg", new File(url));
                }
            }
        });
        mViewModel.ImagesMutableLiveData.observe(this, v -> {
            imageAddAdapter2.getList().add("https://yuncang.dzsmxx.com" + v.getObj());
            imageAddAdapter2.notifyDataSetChanged();
            mediaHelper2.setCurrentImageCount(imageAddAdapter2.getList().size());
        });
        imageAddAdapter2.setImageViewAddListener(view -> {
            openImageDialog = new OpenImageDialog(context);
            openImageDialog.setMediaType(OpenImageDialog.ALBUM)
                    .setOnOpenImageClickListener(mediaHelper2)
                    .builder()
                    .show();
        });
        imageAddAdapter2.setImageViewClearListener((view, position) -> {
            imageAddAdapter2.getList().remove(position);
            imageAddAdapter2.notifyDataSetChanged();
            mediaHelper2.setCurrentImageCount(imageAddAdapter2.getList().size());
        });
        binding.feedBackRecyclerView.setLayoutManager(new FullyGridLayoutManager(this, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        binding.feedBackRecyclerView.setAdapter(imageAddAdapter2);
        //店铺图片
        imageAddAdapter3 = new ImageAddAdapter(this, 1);
        mediaHelper3 = new MediaBuilder(this, this)
                .setImageAlbumCount(1)
                .setImageQualityCompress(0)
                .builder();
        mediaHelper3.getMutableLiveData().observe(this, mediaBean -> {
            if (mediaBean.getMediaType() == MediaTypeEnum.IMAGE.getMediaType()) {
//                imageAddAdapter3.getList().addAll(mediaBean.getMediaList());
//                imageAddAdapter3.notifyDataSetChanged();
//                mediaHelper3.setCurrentImageCount(imageAddAdapter3.getList().size());
                for (String url : mediaBean.getMediaList()) {
                    mViewModel.postDianpuimg("https://yuncang.dzsmxx.com/api/timetable/Basics/updateimg", new File(url));
                }
            }
        });
        mViewModel.DianpuimgMutableLiveData.observe(this, v -> {
            imageAddAdapter3.getList().add("https://yuncang.dzsmxx.com" + v.getObj());
            imageAddAdapter3.notifyDataSetChanged();
            mediaHelper3.setCurrentImageCount(imageAddAdapter3.getList().size());
        });
        imageAddAdapter3.setImageViewAddListener(view -> {
            openImageDialog = new OpenImageDialog(context);
            openImageDialog.setMediaType(OpenImageDialog.ALBUM)
                    .setOnOpenImageClickListener(mediaHelper3)
                    .builder()
                    .show();
        });
        imageAddAdapter3.setImageViewClearListener((view, position) -> {
            imageAddAdapter3.getList().remove(position);
            imageAddAdapter3.notifyDataSetChanged();
            mediaHelper3.setCurrentImageCount(imageAddAdapter3.getList().size());
        });
        binding.StoreRecyclerView.setLayoutManager(new FullyGridLayoutManager(this, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        binding.StoreRecyclerView.setAdapter(imageAddAdapter3);
    }

    public List<Integer> getCheckBox(List<CheckBoxBean.ArrayBean> list) {
        List<Integer> intList = new ArrayList<>();
        for (CheckBoxBean.ArrayBean item : list) {
            if (item.getCheck()) {
                intList.add(item.getId());
            }
        }
        return intList;
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getPeiSong();
        mViewModel.getQuYu();
        mViewModel.getDianPuLeiXing();
        mViewModel.getWeiXin("", true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == RESULT_OK && data != null) {
            Latitude = data.getExtras().getDouble("Latitude");
            Longitude = data.getExtras().getDouble("Longitude");
            AdName = data.getExtras().getString("AdName");
            binding.StoreAddress.setText(AdName);
        } else if (openImageDialog != null) {
            if (openImageDialog.getOnOpenImageClickListener() == mediaHelper1)
                mediaHelper1.onActivityResult(requestCode, resultCode, data);
            if (openImageDialog.getOnOpenImageClickListener() == mediaHelper2)
                mediaHelper2.onActivityResult(requestCode, resultCode, data);
            if (openImageDialog.getOnOpenImageClickListener() == mediaHelper3)
                mediaHelper3.onActivityResult(requestCode, resultCode, data);
        }
    }
}